package shuashuami.hb.com.view;

import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpCMethods;
import shuashuami.hb.com.http.HttpOnNextListener;

/**
 * Created by Administrator on 2017/6/20.
 */

public class SelectBankDialog extends Dialog {

    private Activity activity;
    private TextView tvData;
    private LayoutInflater inflater;
    private LinearLayout ll;
    private String id;

    public SelectBankDialog(Activity context, TextView tvData) {
        super(context, R.style.Dialog);
        inflater = LayoutInflater.from(context);
        this.activity = context;
        this.tvData = tvData;
        setCustomDialog();
    }

    public String getId() {
        return id;
    }

    private void setCustomDialog() {
        View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_select_bank_layout, null);
        ll = (LinearLayout) mView.findViewById(R.id.ll_content);
        getItems();
        super.setContentView(mView);
    }


    @Override
    public void setContentView(int layoutResID) {
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
    }

    @Override
    public void setContentView(View view) {
    }

    public void getItems() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        HttpCMethods.getBankList(listener);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 200) {
                            JSONArray jsonArray = jsonObject.getJSONArray("content");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                final JSONObject temp = jsonArray.getJSONObject(i);
                                View view = inflater.inflate(R.layout.bank_items, null);
                                TextView textView = (TextView) view.findViewById(R.id.tv_select_bank_name_1);
                                View view1 = view.findViewById(R.id.v_isshow);
                                if (i == jsonArray.length() - 1) {
                                    view1.setVisibility(View.GONE);
                                }
                                textView.setText(temp.getString("name"));
                                textView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        try {
                                            id = temp.getString("id");
                                            tvData.setText(temp.getString("name"));
                                            dismiss();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                                ll.addView(view);
                            }


                        } else {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
}
